var express = require('express');
var router = express.Router();
var crypto = require('crypto');

var db = require("./../../utils/db.js");



// 获取用户列表
router.get('/listAll', function (req, res, next) {
    var sql = "select * from base_user where 1=1";

    db.query(sql, function (err, rows) {
        if (rows&&rows.length >=0) {
            res.render('admin/person/listAll', {
                personList:rows
            });
        } else {
            res.render('error', {
                message: err.message,
                error: err
            });
        }
    });

});

// 新增用户页面
router.get('/add', function (req, res, next) {
    res.render('admin/person/edit',  {
        id: '',
        loginName: '',
        personName: '',
        personSex: '',
        personAge: '',
        personContactInformation: ''
    });

});


// 修改用户页面
router.get('/edit/:id', function (req, res, next) {
    var id = req.params.id;
    var sql = "select * from base_user where id = " + id;
    if(id){
        db.query(sql,function(err,rows){
            if(rows && rows.length > 0){
                res.render('admin/person/edit', {
                    id: rows[0].id,
                    loginName: rows[0].loginname,
                    personName: rows[0].person_name,
                    personSex: rows[0].person_sex,
                    personAge: rows[0].person_age,
                    personContactInformation: rows[0].person_contact_information
                });

            }else {
                res.render('admin/person/edit', {
                    id: '',
                    loginName: '',
                    personName: '',
                    personSex: '',
                    personAge: '',
                    personContactInformation: ''
                });
            }
        });
    }


});

//保存
router.post('/save', function (req, res, next) {
    var loginName = req.body.loginName ? req.body.loginName.trim():'' ;
    var personName = req.body.personName ? req.body.personName.trim():'';
    var personSex = req.body.personSex ? req.body.personSex.trim():'';
    var personAge = req.body.personAge ? req.body.personAge.trim():'';
    var personContactInformation = req.body.personContactInformation ? req.body.personContactInformation.trim():'';
    var id = req.body.id;
    var sql = '';
    if (id) {
        sql = "update base_user set loginname = '" + loginName + "',person_name = '" + personName + "',person_sex = '" + personSex + "',person_age = '" + personAge + "',person_contact_information = '" + personContactInformation + "'  where id = " + id + "";
    } else {
        sql = "insert into base_user(loginname,person_name,person_sex,person_age,person_contact_information, team_permission) values('" + loginName + "','" + personName + "','" + personSex + "','" + personAge + "','" + personContactInformation + "', 0)";
    }
    db.query(sql, function (err, rows) {
        if (rows) {
            res.redirect("/person/listAll");
        } else{
            res.send("保存失败"+ err);
        }
    });

});






//用户删除
router.get("/delete/:id",function(req,res){
    var id = req.params.id;
    var sql = "delete from base_user where id =  '" + id + "'";
    db.query(sql,function(err,rows){
        if(err){
            res.send("删除失败"+err);
        }
        else {
                res.redirect("/person/listAll");
        }
    });
});



//用户查询
router.post("/search",function(req,res,next){
    var personName = req.body.personName;
    var personSex = req.body.personSex;
    var sql = "select * from base_user where 1=1 ";
    if(personName){
        sql += "and person_name like '%"+ personName +"%'";
    }
    if(personSex){
        sql += "and person_sex like '%"+ personSex +"%'";
    }
    db.query(sql,function(err,rows){
        if(err){
            res.send("查询失败: "+err);
        }else{
            res.render('admin/person/listAll',{
                title:"用户列表",
                personList:rows,
                personName:personName,
                personSex:personSex
            });
        }
    });
});

module.exports = router;
